//
//  BaseTabBarController.m
//  deerkids
//
//  Created by 王昭的Mac Air on 2017/8/28.
//  Copyright © 2017年 wangzhao. All rights reserved.
//

#import "BaseTabBarController.h"
#import "BaseNaviController.h"
#import "HomeController.h"
#import "TalkController.h"
#import "MessageController.h"
#import "ProfileController.h"
#import "PublishController.h"
#import "LBTabBar.h"
#import "LoginController.h"


@interface BaseTabBarController ()<LBTabBarDelegate>

@end

@implementation BaseTabBarController

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self addControllers];
    }
    return self;
}
// 支持设备自动旋转
- (BOOL)shouldAutorotate
{
    return YES;
}
// 支持横竖屏显示
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}


-(void)viewDidLoad{
    [super viewDidLoad];
    
    //创建自己的tabbar，然后用kvc将自己的tabbar和系统的tabBar替换下
    LBTabBar *tabbar = [[LBTabBar alloc] init];
    tabbar.myDelegate = self;
    //kvc实质是修改了系统的_tabBar
    [self setValue:tabbar forKeyPath:@"tabBar"];
}

- (void)addControllers
{
    HomeController *homeVC = [[HomeController alloc] init];
    [self addChildController:homeVC title:@"首页" image:@"nav_home" selectedImage:@"nav_home_sel"];
    
    
    TalkController *talkVC = [[TalkController alloc] init];
    [self addChildController:talkVC title:@"讨论组" image:@"nav_talk" selectedImage:@"nav_talk_sel"];
    
    MessageController *messageVC = [[MessageController alloc] init];
    [self addChildController:messageVC title:@"消息" image:@"nav_message" selectedImage:@"nav_message_sel"];
 
    
    ProfileController *settingVC = [[ProfileController alloc] init];
    [self addChildController:settingVC title:@"我的" image:@"nav_mine" selectedImage:@"nav_mine_sel"];
}

- (void)addChildController:(UIViewController *)viewController title:(NSString *)title image:(NSString *)imageName selectedImage:(NSString *)selectedImageName
{
    viewController.title = title;
    
    
    [viewController.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -3)];  //调节文字和图片的间距
    [viewController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:UIColorFromRGB(Text6Color)} forState:UIControlStateNormal];  //设置文字的默认颜色和选中颜色
    [viewController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:UIColorFromRGB(BgTitleColor)} forState:UIControlStateSelected];
    
    viewController.tabBarItem.image = [UIImage imageNamed:imageName];  //设置默认图片和选中图片
    viewController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    BaseNaviController *naviVC = [[BaseNaviController alloc] initWithRootViewController:viewController];  //将vc设置为navi vc的根控制器
    
    [self addChildViewController:naviVC];  //将navi vc添加到tabBar vc中
}




#pragma mark - ------------------------------------------------------------------
#pragma mark - LBTabBarDelegate
//点击中间按钮的代理方法
- (void)tabBarPlusBtnClick:(LBTabBar *)tabBar
{
    DLog(@"点击了发布");
    if([UserUtil getUser].isOnline){
        PublishController *profileVC = [[PublishController alloc] init];
        
        //将vc设置为navi vc的根控制器
        BaseNaviController *naviVC = [[BaseNaviController alloc] initWithRootViewController:profileVC];
        [self presentViewController:naviVC animated:YES completion:^{
            
        }];
    }else{
        LoginController *profileVC = [[LoginController alloc] init];
        
        //将vc设置为navi vc的根控制器
        BaseNaviController *naviVC = [[BaseNaviController alloc] initWithRootViewController:profileVC];
        [self presentViewController:naviVC animated:YES completion:^{
            
        }];
    }
}


- (UIColor *)randomColor
{
    CGFloat r = arc4random_uniform(256);
    CGFloat g = arc4random_uniform(256);
    CGFloat b = arc4random_uniform(256);
    return [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
